package sk.activities;

import java.util.ArrayList;
import java.util.List;
import sk.model.Player;
import sk.model.Session;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ShowSessionsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);

		buildTable(getSessions());

	}

	private List<Session> getSessions() {
		List<Session> sessions = new ArrayList<Session>();
		SharedPreferences storage =
			getApplicationContext().getSharedPreferences(
				SessionActivity.STORAGE_NAME, MODE_PRIVATE);
		int sessionCount =
			storage.getInt(SessionActivity.SAVE_SESSION_COUNT, 0);
		for (int i = 0; i < sessionCount + 1; i++) {
			String sessionString =
				storage.getString(SessionActivity.SAVE_SESSION + i, "");
			if (!sessionString.equals("")) {
				sessions.add(new Session(sessionString));
			}
		}
		return sessions;
	}

	private void buildTable(List<Session> sessions) {
		List<String> sessionStrings = new ArrayList<String>();
		for (Session session : sessions) {
			String s =
				"Session " + session.getID() + " -> "
					+ session.getGames().size();
			if (session.getGames().size() == 1) {
				s += " Spiel" + "\n";
			} else {
				s += " Spiele" + "\n";
			}
			for (Player player : session.getPlayers()) {
				s += player.getName() + " " + session.getResult(player) + "\n";
			}
			sessionStrings.add(s);
		}

		// create List to show sessions
		final ListView playerList = (ListView) findViewById(R.id.showListView);
		final ListAdapter playerListAdapter =
			new ArrayAdapter<String>(getApplicationContext(),
				R.layout.show_list_item, sessionStrings);
		playerList.setAdapter(playerListAdapter);

		playerList.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int itemID, long arg3) {
				showDeleteDialog((String) playerList.getItemAtPosition(itemID));
				return false;
			}

		});

		playerList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int itemID,
				long arg3) {
				openStatsActivity((String) playerList.getItemAtPosition(itemID));
			}
		});
	}

	protected void openStatsActivity(final String s) {
		// get session string
		String[] tokens = s.split(" ");
		int id = Integer.parseInt(tokens[1]);
		SharedPreferences storage =
			getApplicationContext().getSharedPreferences(
				SessionActivity.STORAGE_NAME, MODE_PRIVATE);
		String sessionString =
			storage.getString(SessionActivity.SAVE_SESSION + id, "");

		Intent intent = new Intent(this, StatsActivity.class);
		intent.putExtra(SessionActivity.EXTRA_SESSION_FOR_STATS, sessionString);
		startActivity(intent);
	}

	protected void showDeleteDialog(final String s) {
		// Create Dialog to get device name
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getString(R.string.delete_session));

		// Set up the buttons
		builder.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				deleteSession(s);
			}
		});
		builder.setNegativeButton("Nein", null);
		builder.show();
	}

	protected void deleteSession(String s) {
		String[] tokens = s.split(" ");
		int id = Integer.parseInt(tokens[1]);
		SharedPreferences storage =
			getApplicationContext().getSharedPreferences(
				SessionActivity.STORAGE_NAME, MODE_PRIVATE);
		SharedPreferences.Editor editor = storage.edit();
		editor.putString(SessionActivity.SAVE_SESSION + id, "");
		editor.commit();
		buildTable(getSessions());
	}
}